<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-s-check"></i> 员工列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-tabs v-model="activeName">
                <!-- 点击员工列表 -->
                <el-tab-pane label="员工列表" name="first">
                    <div class="handle-box">
                        <el-input placeholder="请输入-账号/用户名" class="handle-input mr10" v-model="namekey"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    </div>
                    <!-- 员工表格 -->
                    <el-table :data="userlist" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                        <el-table-column prop="empl_no" label="账号" width="180" align="center"></el-table-column>
                        <el-table-column prop="empl_name" label="用户名" align="center"></el-table-column>
                        <el-table-column prop="open_sum" label="绑定数量" align="center"></el-table-column>
                        <el-table-column prop="user_maxnumer" label="绑定数量上限" align="center"> </el-table-column>
                        <el-table-column label="后台登陆权限" align="center" width="160">
                            <!-- 插槽 -->
                            <template slot-scope="scope">
                                <el-switch
                                    v-model="scope.row.status"
                                    active-text="允许"
                                    inactive-text="不允许"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    @change="changeSwitch($event, scope.row, scope.$index)"
                                ></el-switch>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="400" align="center">
                            <template slot-scope="scope">
                                <el-button @click="btnView(scope.row)">查看绑定</el-button>
                                <el-button type="warning" plain @click="btnChange(scope.row)">更改绑定数量</el-button>
                                <el-button type="primary" plain @click="btnObtain(scope.row)">获取验证码</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <!-- 分页 -->
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="queryinfo.page"
                        :page-size="10"
                        layout="total,prev, pager, next, jumper"
                        :total="428"
                    >
                    </el-pagination>
                </el-tab-pane>
                <!-- 查看绑定 -->
                <el-tab-pane label="查看绑定" name="second">
                    <template v-if="activeName === 'second'">
                        <div class="handle-box">
                            <el-input placeholder="请输入-账号/用户名" class="handle-input mr10"></el-input>
                            <el-button type="primary" icon="el-icon-search">搜索</el-button>
                        </div>
                        <!-- 搜索框 -->
                        <!-- 查看绑定表格 -->
                        <el-table border style="width: 100%" :data="bindinglist">
                            <el-table-column prop="wechat_name" label="微信名" width="300" align="center"> </el-table-column>
                            <el-table-column prop="img" label="头像（查看大图）" width="750" align="center">
                                <template slot-scope="scope">
                                    <el-image
                                        style="width: 100px; height: 100px"
                                        :src="scope.row.img"
                                        :preview-src-list="srcList"
                                    ></el-image>
                                </template>
                            </el-table-column>
                            <el-table-column prop="end_time" label="到期时间" width="300" align="center"> </el-table-column>
                            <el-table-column label="操作" align="center">
                                <template slot-scope="scope">
                                    <el-button type="danger" plain @click="btnUnbundling(scope.row)">解绑</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <!-- 查看表格分页 -->
                        <div class="block">
                            <el-pagination :page-size="100" layout="total, prev, pager, next" :total="count"> </el-pagination>
                        </div>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </div>
        <!-- 获取验证码弹出框 -->
        <el-dialog id="dialogbox" title="验证码" :visible.sync="dialogVisible" width="30%" @close="closeDialog">
            <!-- 内容区 -->
            <div v-if="changebox">
                <span class="span1">不填则为永久账号，填入时间为体验账号</span>
                <div class="inputhour">
                    <span>有效时间</span>
                    <el-input class="hadle1" placeholder="以小时为单位" v-model="sum" @blur="BlurText($event)" clearable> ></el-input>
                </div>
            </div>
            <!-- 内容区1 -->
            <div v-else>
                <div class="claas1 class5">
                    账号 <span>{{ verificationlist.user_no }}</span>
                </div>
                <div class="claas2 class5">
                    用户名 <span>{{ verificationlist.user_name }}</span>
                </div>
                <div class="claas3 class5">
                    提示码 <span>{{ verificationlist.random }}</span>
                </div>
                <div class="claas4 class5">
                    提示 <span>{{ verificationlist.message }}</span>
                </div>
            </div>

            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="btnckick">确 定</el-button>
            </span>
        </el-dialog>
        <!--获取更改绑定数量弹出框 -->
        <el-dialog title="更改绑定上限" :visible.sync="dialogVisiblechange" width="30%">
            <div class="inputchange">
                <span>绑定上限</span>
                <el-input
                    class="hadle1"
                    v-model="user_maxnumer"
                    placeholder="请输入内容"
                    onkeyup="this.value=this.value.replace(/[^\d.]/g,'');"
                ></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisiblechange = false">取 消</el-button>
                <el-button type="primary" @click="btnBinding">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'basetable',
    data() {
        return {
            activeName: 'first',
            // 获取用户列表的参数
            queryinfo: {
                token: '',
                page: 1,
                page_size: 10,
                keyword: this.namekey
            },
            // 保存用户列表
            userlist: [],
            // 总头数
            total: 428,
            dialogVisible: false,
            sum: '',
            changebox: true,
            // 获取验证码
            verificationlist: [],
            empl_no: '',
            //更改数量弹出框
            dialogVisiblechange: false,
            // 该改参数
            user_maxnumer: '',
            //更改列表
            changlist: [],
            namekey: '',
            //绑定列表
            bindinglist: [],
            srcList: [],
            open_id: '',
            count: 0,
            //解绑
            Unbundlinglist: [],
            status: 0,
            customerlist: []
        };
    },
    created() {
        this.getkey()
        this.getuserlist();
        this.customer()
    },
    methods: {
        getkey() {
            this.queryinfo.token = sessionStorage.getItem('token')
        },
        async getuserlist() {
            const { data: res } = await this.$axios.get('admin/admin_list', {
                params: this.queryinfo
            });
            if (res.code !== 2000) {
                return this.$message.error('获取用户列表失败');
            }
            this.userlist = res.data.data;
            console.log(this.userlist)
            this.empl_no = res.data.data[0].empl_no;
            this.user_maxnumer = res.data.data[0].user_maxnumer;
        },
        //客户列表参数
        async customer() {
         const { data: res }  = await this.$axios.get('admin/admin_list', {
                params: {
                    token: 'MDAwMDAwMQ==-tbrj',
                    page: 1,
                    page_size: 10,
                    keyword: this.namekey
                }
            });
            if (res.code !== 2000) {
                return this.$message.error('获取用户列表失败');
            }
            this.userlist = res.data.data;
            this.empl_no = res.data.data[0].empl_no;
            this.user_maxnumer = res.data.data[0].user_maxnumer;
            this.userlist = res.data.data;
            // console.log("this.namekey",this.namekey)
            
        },
        // switch开关
        changeSwitch(e, row, index) {
            console.log(e, row, index);
            let uId = row.index;
            this.getuserlist();
        },
        //限制输入框只能输入正整数
        BlurText(e) {
            let boolean = new RegExp('^[1-9][0-9]*$').test(e.target.value);
            if (!boolean) {
                this.$message.error('请输入正整数');

                e.target.value = ' ';
            }
        },
        // 触发搜索按钮
        handleSearch() {
            this.keyword = this.namekey
            this.customer()
            // console.log(this.keyword)
        },
        // 监听pagesize改变的事件
        handleSizeChange(newSize) {
            this.queryinfo.page = newSize;
            this.getuserlist();
        },
        // 监听页码值的改变的事件
        handleCurrentChange(newPage) {
            this.queryinfo.newPage = newPage;
            this.getuserlist();
        },
        // 点击获取验证码
        btnObtain(row) {
            this.dialogVisible = true;
            this.empl_no = row.empl_no;
        },
        // 更改数量
        btnChange(row) {
            // console.log(row);
            this.dialogVisiblechange = true;
        },
        // 绑定列表
        async btnView(row) {
            this.activeName = 'second';
            const { data: res } = await this.$axios.get('admin/admin_open_user', {
                params: {
                    token: 'MDAwMDAwMQ==-tbrj',
                    empl_no: this.empl_no,
                    page: this.queryinfo.page,
                    page_size: this.queryinfo.page_size,
                    keyword: this.namekey
                }
            });
            this.bindinglist = res.data.data;
            this.count = res.data.count;
            this.open_id = res.data.data[0].open_id;
            console.log(this.bindinglist);
            this.srcList = res.data.data.map((item) => {
                return item.img;
            });
        },
        // 解绑
        btnUnbundling(row) {
            this.$confirm('确定要解除绑定吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then((action) => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    if (action === 'confirm') {
                        this.Unbundlinglist = this.$axios.get('admin/open_user_delete', {
                            params: {
                                token: 'MDAwMDAwMQ==-tbrj',
                                open_id: this.open_id
                            }
                        });
                        this.btnView();
                    }
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        // 点击确定，更改数量
        async btnBinding() {
            const { data: res } = await this.$axios.get('admin/set_max_user', {
                params: {
                    token: 'MDAwMDAwMQ==-tbrj',
                    empl_no: this.empl_no,
                    user_maxnumer: this.user_maxnumer
                }
            });
            this.changlist = res;
            if (res.code !== 2000) {
                this.$message.error('修改失败');
                return;
            }
            this.$message.success('修改成功');
            this.getuserlist();
            this.dialogVisiblechange = false;
        },
        closeDialog() {
            this.sum = '';
            this.dialogVisible = false;
            this.changebox = true;
        },
        async btnckick() {
            // console.log(this.changebox)
            console.log('我是废物');
            console.log(this.dialogVisible);
            console.log(this.sum);
            if (this.sum === '') {
                this.$message.error('请输入时间');
                return;
            }
            this.changebox = false;
            const { data: res } = await this.$axios.get('admin/send_v_code', {
                params: {
                    token: 'MDAwMDAwMQ==-tbrj',
                    empl_no: this.empl_no,
                    sum: this.sum
                }
            });
            this.verificationlist = res.data;
            console.log(this.verificationlist);
            console.log(this.changebox);
        },
        clear() {
            this.sum = '';
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.inputhour {
    display: flex;
    justify-content: space-between;
}
.table {
    width: 100%;
    font-size: 14px;
}
.hadle1 {
    width: 80%;
}
.span1 {
    color: gainsboro;
    margin-top: -30px;
    position: absolute;
}
.el-pagination {
    margin-top: 10px;
}
#dialogbox {
    text-align: left;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
